package com.koron.order.overhaul.controller;

import com.alibaba.fastjson.JSON;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.annocation.CasUser;
import com.koron.order.common.bean.po.OrderInstructionBean;
import com.koron.order.overhaul.bean.vo.OverhaulOrderDraftVO;
import com.koron.order.overhaul.service.OverhaulOrderService;
import com.koron.order.overhaul.utils.CommonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author twcao
 * @title: OverhaulOrderDraftController
 * @projectName GDH_EAM_V2
 * @description: 大修工单-草稿
 * @date 2021/10/219:03
 */
@Slf4j
@Api(tags = "大修工单-草稿")
@RestController
@RequestMapping("/overhaul/draft")
public class OverhaulOrderDraftController {

    @Autowired
    private OverhaulOrderService overhaulOrderService;

    @ApiOperation(value = "草稿(status = 0)，新增或修改(基本信息，专业，表单，作业准备，作业过程，作业指导书)，返回工单id")
    @PostMapping("/saveOrUpdate")
    public Response<String> draft(@CasUser @ApiIgnore EamUser user, @RequestBody OverhaulOrderDraftVO vo) {
        log.info("草稿-新增或修改，vo = {}", JSON.toJSONString(vo));
        vo.setDeptId(StringUtils.isBlank(vo.getDeptId()) ? user.getOrgId() : vo.getDeptId());
        CommonUtils.update(vo, user);
        return Response.ok("操作成功", ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "draft", String.class, vo));
    }

    @ApiOperation(value = "草稿(status = 0)，新增或修改(作业指导书)，会先删除之前的")
    @PostMapping("/instruction")
    public Response<Boolean> instruction(@CasUser @ApiIgnore EamUser user, @RequestBody List<OrderInstructionBean> instructions) {
        log.info("草稿，新增或修改(作业过程)，instructions = {}", JSON.toJSONString(instructions));
        return Response.ok("操作成功", ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "saveOrUpdateInstruction", Boolean.class, instructions));
    }

    @ApiOperation(value = "草稿(status = 0)，删除作业指导书")
    @DeleteMapping("/instruction")
    public Response<Boolean> instruction(@CasUser @ApiIgnore EamUser user, String instructionId) {
        log.info("草稿，删除作业指导书，instructionId = {}", instructionId);
        return Response.ok("操作成功", ADOConnection.runTask(user.getCurrDs(), overhaulOrderService, "deleteInstruction", Boolean.class, instructionId));
    }
}
